<?php
/**
 * User: dimka3210
 * Date: 21.11.13
 * Time: 11:14
 */
include_once('../core/autoload.php');
?>
<html>
<head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="/static/css/my.css">
</head>
<body>
<?php
$user = new User();
$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
if (!$user->checkAdminRight()) {
    header('Location: /');
}

$storage = Storage::getInstance();

if ($_POST) {
    $_id = $storage->saveRow($_POST);
    header('Location: /admin/');
}

if ($id) {
    $row = $storage->findRowById($id);

    if (!$row) {
        die('<h1>There is no such record</h1>');
    }
} else {
    $row = array();
}

$f = new Form();
?>
<form method="post">
    <table class="content edit">
        <tr>
            <td style="width: 150px">Title:</td>
            <td><input type="text" name="title" value="<?= $f->checkFields($row, 'title') ?>"></td>
        </tr>
        <tr>
            <td>Text:</td>
            <td><textarea name="text"><?= $f->checkFields($row, 'text') ?></textarea></td>
        </tr>
        <tr>
            <td>Active:</td>
            <td><input type="checkbox" value="1"
                       name="is_active" <?= (!$f->checkFields($row, 'is_active')) ? : 'checked="checked"' ?>></td>
        </tr>
        <tr>
            <td colspan="2"><input type="submit"></td>
        </tr>
    </table>
    <?php if ($f->checkFields($row, 'id'))
        echo '<input type="hidden" name="id" value="' . $row['id'] . '">';
    echo '<input type="hidden" name="create_date" value="' . (isset($row['create_date']) ? $row['create_date'] : '') . '">';
    ?>
</form>
</body>
</html>
